package trial;

import java.util.*;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/27 18:49
 * @描述 华子笔试
 */
public class HuaweiCo {
    public static void main(String[] args) {
        test();
//        String s="123*mby";
//        System.out.println(s.substring(0,3));
//        List<Integer> ans=new ArrayList<>();
//        ans.add(1);
//        ans.add(3);
//        ans.add(7);
//        System.out.println(Collections.max(ans));
//        test();
    }

    public static void test2() {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        for (int i = 0; i < num; i++) {
            System.out.println(judge(sc.next()));
        }
    }

    public static int judge(String str) {
        int mutiply = str.indexOf("*");
        int equals = str.indexOf("=");
        String str1 = str.substring(0, mutiply);
        String str2 = str.substring(mutiply + 1, equals);
        String str3 = str.substring(equals + 1, str.length());
        int value1Len = str1.length();
        int value2Len = str2.length();
        int resultLen = str3.length();
        if(value1Len+value2Len-1>resultLen){
            return -1;
        }
        int value1 = Integer.parseInt(str1);
        int value2 = Integer.parseInt(str2);
        long result = Long.parseLong(str3);
        long gap = result - (long) value1 * value2;
        int gapLen=Integer.parseInt(String.valueOf(gap));
        int ans=0;
        //差距为零直接返回
        if (gap == 0) {
            return 0;
        }
        //标记三个数字是否移动到极限
        boolean tag1=false,tag2=false,tag3=false;
        while((!tag1||!tag2||!tag3)&&gap!=0)
        if(resultLen>=gapLen){
            if(Integer.parseInt(String.valueOf(result-gap))==resultLen){
                gap=0;
                ans=ans+1;
            }else {
                ans=ans+1;
                if(gap>0){
                    gap=gap-(result-get(resultLen));
                }else {
                    gap=gap+(get(resultLen+1)-result);
                }
            }
        }




        if(gap==0){
            return ans;
        }else {
            return -1;
        }

    }


    public static int get(int i){
        int ans=10;
        for (int j = 1; j < i; j++) {
            ans*=10;
        }
        return ans;
    }
    public static void test3() {
        Scanner sc = new Scanner(System.in);
        int projectNum = sc.nextInt();
        int front = sc.nextInt(), back = sc.nextInt(), test = sc.nextInt();
        int[] values = new int[projectNum];
        for (int i = 0; i < projectNum; i++) {
            values[i] = sc.nextInt();
        }
        Need[] needs = new Need[projectNum];
        for (int i = 0; i < projectNum; i++) {
            needs[i] = new Need(sc.nextInt(), sc.nextInt(), sc.nextInt());
        }
        List<Integer> answers = new ArrayList<>();
        for (int i = projectNum; i > 0; i--) {

            boolean[] selected = new boolean[projectNum];
            int ans;

//            answers.add(ans);
        }
        int max = 0;
        for (int i : values
        ) {
            max += i;
        }
        answers.add(max);
        System.out.println(Collections.max(answers));
    }

    //    public static HashSet<HashSet<Integer>> findWay(int[] nums,int select){
//        int len=nums.length;
//        if(select>len){
//            return null;
//        }
//        HashSet<HashSet<Integer>> ans=new HashSet<>();
//        return ans;
//    }
    static class Need {
        private int[] needs;

        Need(int a, int b, int c) {
            needs = new int[3];
            needs[0] = a;
            needs[1] = b;
            needs[2] = c;
        }

        public int getFront() {
            return needs[0];
        }

        public int getBack() {
            return needs[1];
        }

        public int getTest() {
            return needs[2];
        }
    }


    public static void test1() {
        Scanner sc = new Scanner(System.in);
        int hour = sc.nextInt();
        int supermarket = sc.nextInt();
        int[] lowPrice = new int[hour];
        Arrays.fill(lowPrice, Integer.MAX_VALUE);
        for (int i = 0; i < supermarket; i++) {
            int start = sc.nextInt();
            int end = sc.nextInt();
            int price = sc.nextInt();
            for (int j = start - 1; j < end; j++) {
                if (j >= 0 && j < hour) {
                    lowPrice[j] = Math.min(lowPrice[j], price);
                }
            }
        }
        int ans = 0;
        for (int i : lowPrice) {
            ans += i;
        }
        System.out.println(ans);
    }

    public static void test() {
        Scanner sc = new Scanner(System.in);
        int hour = sc.nextInt();
        int supermarket = sc.nextInt();
        List<Supermarket> list = new ArrayList<>();
        for (int i = 0; i < supermarket; i++) {
            int start = sc.nextInt();
            int end = sc.nextInt();
            int price = sc.nextInt();
            list.add(new Supermarket(start, end, price));
        }
        int[] lowPrice = new int[hour];
        System.out.println(list);
        Collections.sort(list, Comparator.comparingInt(Supermarket::getPrice));
        System.out.println(list);
        boolean[] visted = new boolean[hour];
        Arrays.fill(visted, false);
        for (int i = 0; i < list.size(); i++) {
            for (int j = list.get(i).getStart() - 1; j < list.get(i).getEnd(); j++) {
                if (!visted[j]) {
                    lowPrice[j] = list.get(i).getPrice();
                    visted[j] = true;
                }
            }
            System.out.println("listprice:" + list.get(i).getPrice());
            System.out.println(Arrays.toString(lowPrice));
            System.out.println(Arrays.toString(visted));
        }
//        for (Supermarket s: list) {
//            for(int i=s.getStart()-1;i<=s.getEnd()-1;i++){
//                if(i>=0&&i<lowPrice.length){
//                    int value=lowPrice[i];
//                    int price=s.getPrice();
//                    if(value>price){
//                        lowPrice[i]=price;
//                    }
//                }
//            }
//        }
        System.out.println(Arrays.toString(lowPrice));
        int sum = 0;
        for (int i : lowPrice
        ) {
            if (i != Integer.MAX_VALUE) {
                sum += i;
            }
        }

        System.out.println(sum);
    }

    static class Supermarket {
        private int start;
        private int end;
        private int price;

        Supermarket(int start, int end, int price) {
            this.start = start;
            this.end = end;
            this.price = price;
        }

        public int getStart() {
            return start;
        }

        public void setStart(int start) {
            this.start = start;
        }

        public int getEnd() {
            return end;
        }

        public void setEnd(int end) {
            this.end = end;
        }

        public int getPrice() {
            return price;
        }

        public void setPrice(int price) {
            this.price = price;
        }
    }
}
